package com.tcwlkj.uuService.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.tcwlkj.uuService.R;
import com.tcwlkj.uuService.model.MessageModel;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/11/1 0001.
 */

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.ViewHolder> implements View.OnClickListener{


    private ArrayList<MessageModel> mMsgList;

    public MessageAdapter(ArrayList<MessageModel> mMsgList) {
        this.mMsgList = mMsgList;
    }

    public void updateData(ArrayList<MessageModel> data) {
        this.mMsgList = data;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = View.inflate(parent.getContext(), R.layout.item_message, null);
        view.setOnClickListener(this);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.itemView.setTag(position);
        holder.tv_title.setText(mMsgList.get(position).getTitle());
        holder.tv_time.setText(mMsgList.get(position).getAddTime());
        holder.tv_content.setText(mMsgList.get(position).getPopup());
    }

    @Override
    public int getItemCount() {
        return mMsgList.size();
    }

    private OnItemClickListener mOnItemClickListener = null;
    public static interface OnItemClickListener {
        void onItemClick(View view , int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_title;
        TextView tv_time;
        TextView tv_content;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            tv_time = (TextView) itemView.findViewById(R.id.tv_time);
            tv_content = (TextView) itemView.findViewById(R.id.tv_content);
        }
    }
}
